package com.shisfish.english.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.shisfish.core.common.GlobalVariable;
import com.shisfish.core.dto.PageData;
import com.shisfish.core.dto.ResultDto;
import com.shisfish.english.model.SingleWord;
import com.shisfish.english.service.ISingleWordService;
import com.shisfish.english.transform.ISingleWordTransform;

@Service(value = "singleWordService")
public class SingleWordServiceImpl implements ISingleWordService{

	@Resource 
	private ISingleWordTransform singleWordTransform;
	
	@Override
	public ResultDto<Integer> insert(String name, String meaning) {
		// 判重
		SingleWord single = singleWordTransform.selectByName(name);
		if (single != null)
			return new ResultDto<>(GlobalVariable.SAME_CODE, GlobalVariable.SAME_MESSAGE);
		SingleWord singleWord = new SingleWord(name, meaning);
		int result = singleWordTransform.insertObj(singleWord);
		if (result == 0) 
			return new ResultDto<>(GlobalVariable.OPERATION_CODE, GlobalVariable.OPERATION_MESSAGE);
		return new ResultDto<Integer>(result);
	}

	@Override
	public ResultDto<List<SingleWord>> searchByNameL(String name) {
		List<SingleWord> singleWordList = singleWordTransform.selectByNameL(name);
		if (singleWordList == null || singleWordList.size() == 0)
			singleWordList = new ArrayList<>();
		return new ResultDto<>(singleWordList);
	}

	@Override
	public ResultDto<Integer> changeMeaning(Long id, String meaning) {
		int result = singleWordTransform.changeMeaning(id, meaning);
		if (result == 0) 
			return new ResultDto<>(GlobalVariable.OPERATION_CODE, GlobalVariable.OPERATION_MESSAGE);
		return new ResultDto<Integer>(result);
	}

	@Override
	public ResultDto<Integer> deleteById(Long id) {
		
		int result = singleWordTransform.deleteById(id);
		if (result == 0) 
			return new ResultDto<>(GlobalVariable.OPERATION_CODE, GlobalVariable.OPERATION_MESSAGE);
		return new ResultDto<Integer>(result);
	}

	@Override
	public ResultDto<List<SingleWord>> searchByParams(String name) {
		List<SingleWord> singelWordList = singleWordTransform.selectByParams(name);
		if (singelWordList == null || singelWordList.size() == 0)
			singelWordList = new ArrayList<>();
		return new ResultDto<List<SingleWord>>(singelWordList);
	}

	@Override
	public ResultDto<List<SingleWord>> pageByParams(Integer pageIndex, Integer pageSize, String name) {
		int index = pageIndex == null ? GlobalVariable.PAGEINDEX : pageIndex;
		int size = pageSize == null ? GlobalVariable.PAGESIZE : pageSize;
		int pageBeginIndex = (index - 1) * pageSize;
		List<SingleWord> singleWordList = singleWordTransform.pageByParams(pageBeginIndex, size, name);
		if (singleWordList == null || singleWordList.size() == 0)
			singleWordList = new ArrayList<>();
		return new ResultDto<List<SingleWord>>(singleWordList);
	}

	@Override
	public ResultDto<PageData> getPageData(Integer pageIndex, Integer pageSize, String name) {
		int count = singleWordTransform.countByParams(name);
		PageData pageData = PageData.createPage(count, pageIndex == null ? 0 : pageIndex, pageSize == null ? 0 : pageSize);
		return new ResultDto<PageData>(pageData);
	}

}
